package cn.kattomy.shop.product.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.kattomy.shop.category.service.CategoryService;
import cn.kattomy.shop.categorysecond.service.CategorySecondService;
import cn.kattomy.shop.product.service.ProductService;
import cn.kattomy.shop.product.vo.Product;
import cn.kattomy.shop.utils.PageBean;

public class ProductAction extends ActionSupport implements ModelDriven<Product> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 用于接收数据的模型驱动
	 */
	private Product product = new Product();
	
	/**
	 * 商品 Service
	 */
	private ProductService productService;
	
	/**
	 * 一级分类的 id
	 */
	private Integer cid = null;
	
	/**
	 * 一级分类 Service
	 */
	private CategoryService categoryService;
	
	/**
	 * 二级分类的 id
	 */
	private Integer csid = null;
	
	/**
	 * 二级分类 Service
	 */
	private CategorySecondService categorySecondService;
	
	/**
	 * 当前页码
	 */
	private int pageNo;
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageNo() {
		return pageNo;
	}


	/**
	 * 搜索的关键字
	 */
	private String keyword;
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public String getKeyword() {
		return keyword;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	public void setProduct(Product product) {
		this.product = product;
	}

	public void setCid(Integer cid) {
		this.cid = cid;
	}
	
	public Integer getCid() {
		return cid;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	@Override
	public Product getModel() {
		return product;
	}
	
	public Integer getCsid() {
		return csid;
	}

	public void setCsid(Integer csid) {
		this.csid = csid;
	}

	public void setCategorySecondService(CategorySecondService categorySecondService) {
		this.categorySecondService = categorySecondService;
	}

	/**
	 * 根据商品 Pid 进行查询商品的执行方法 
	 * @return
	 */
	public String findByPid(){
		product = productService.findByPid(product.getPid());
		return "findByPid";
	}

	/**
	 * 根据商品一级分类的 cid 进行查询一级分类对象关联旗下二级分类集合以及旗下商品的执行方法
	 * @return
	 */
	public String findByCid() {
//		List<Category> cList = categoryService.getAll();
		csid = null;
		PageBean<Product> pageBean = productService.findByPageCid(cid, pageNo);
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findByCid";
	}
	
	/**
	 * 根据商品的二级分类的 csid 进行查询二级分类对象下的商品的执行方法的
	 * @return
	 */
	public String findByCsid() {
		//根据二级分类 csid 以及 当前页码 查询商品
		cid = null;
		PageBean<Product> pageBean = productService.findByPageCsid(csid, pageNo);
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findByCsid";
	}
	
	/**
	 * 搜索模块的 action 根据关键字进行搜索
	 * @return
	 */
	public String search(){
//		productService.createIndexs();
		cid = null;
		csid = null;
		PageBean<Product> pageBean = productService.searchByKeyword(pageNo, keyword);
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "search";
	}
	
}
